/**********************************************************
 * test.c
 *
 *  Copy right (c) 2013, BURCH Co., Ltd.
 *
 *  Created on: 2013-1-1
 *      Author: Elvis.zhang
 *     Version: v0.0.0
 *
 *********************************************************/
#include <stdio.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <errno.h>
#include <string.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <poll.h>

#include <unistd.h>
#include <sys/select.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <pthread.h>

#include "../../davinci_spi1.h" 
#include "ringbuffer.h"

FILE 			*file;
RingData RingbufObj;
typedef struct _tagUserBuf{
				char *buf;
				unsigned int len;
}UsrBufObj, *UsrBufHandler;

int pipeSpi2Net[2];
int pipeNet2Spi[2];

static FILE* orgTsfp = NULL;
void saveOrgTs(char* buf, int len)
{
				if(orgTsfp == NULL)
				{
								orgTsfp = fopen("org.ts", "wb");
				}

				fwrite(buf, 1, len, orgTsfp);
				fflush(orgTsfp);
}

#define FPGA_REST  6

void NetSendThrd(void *arg)
{
				unsigned char  key = 0;
				int count = 0;
				unsigned char check_buf[188];
				unsigned len = 0;
				int i;


				while(1)
				{
								while(key != 0x47)
												RdRingData(&RingbufObj, &key, 1);

								if(key == 0x47)
								{
												while(len <= 187)
												{
																len = GetDataLen(&RingbufObj);
												}
												len = 0;
												RdRingData(&RingbufObj, check_buf, 187);
												for(i = 5;i < 187;i++)
												{
																if(check_buf[5] != check_buf[i])
																{
																				printf("Data error: 0x%x-0x%x\n", check_buf[5], check_buf[i]);
																}
												}
								}
								key = 0x0;

				}
}

void SpiRevThrd(void *arg)
{
				UsrBufHandler wtbuf;
				unsigned int r_size = 0;
				int				fd;
				SPIBufInfo		bufinfo;
				unsigned int i=0;

				printf("in spi thread...\n");
				fd = open("/dev/dm368_spi1",O_RDWR);
				//fd = open(argv[1],O_RDWR);
				printf("**********open dm368_spi1 ok!***********\n");


				ioctl(fd,SPI_GET_BUFINFO,&bufinfo);
				printf("buf info:%d x %d\n",bufinfo.nNum,bufinfo.nSize);

				bufinfo.spi_buf_ptr = (PSPIBuf)malloc(sizeof(SPIBuf) * bufinfo.nNum);
				ioctl(fd,SPI_GET_BUFDETAIL,&bufinfo);
				printf("buf info:%d x %d\n",bufinfo.nNum,bufinfo.nSize);

				printf("---SPI_STREAM_ON = %d----\n",SPI_STREAM_ON);
				ioctl(fd,SPI_STREAM_ON,0);
				/**    fpga reset   **/
				ioctl(fd,FPGA_REST,0);

				wtbuf = malloc(sizeof(UsrBufObj));
				if(wtbuf != NULL)
				{
								wtbuf->buf = malloc(bufinfo.nSize);
				}

				while(1)
				{
								r_size =read(fd,wtbuf->buf,bufinfo.nSize);
								if(r_size != 0)
								{
												WrRingData(&RingbufObj, wtbuf->buf, r_size);
								}
				}
}


int main(int argc,char* argv[])
{

				int				i=0,j=0,idx;
				pthread_t 		tid1,tid2;

				CreateRingData_Linux(&RingbufObj, 1024 * 1024 * 6);
				if( (pipe(pipeSpi2Net) < 0) || (pipe(pipeNet2Spi) < 0) )
								perror("pipe");

				pthread_create(&tid1,NULL, (void *)NetSendThrd, NULL);
				pthread_create(&tid2,NULL, (void *)SpiRevThrd, NULL);

#if  0
				file = fopen("org.ts","r+");
				if(file == NULL)
				{
								printf("open fail!\n");
				}
#endif

				pthread_join(tid1,NULL);
				pthread_join(tid2,NULL);

				printf("*****exit...\n");

				fclose(file);
				close(pipeSpi2Net[0]);
				close(pipeSpi2Net[1]);
				close(pipeNet2Spi[0]);
				close(pipeNet2Spi[1]);
				return 0;
}

